clear all 
capture log close

global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"


set more off
cd "$dataclean"
use incidence_priceanal.dta , clear

cd "$output"

gen week4202015=weekly==2875
gen week4202016=weekly==2927
gen week4202017=weekly==2979
gen weekafter4202015=weekly==2876
gen weekafter4202016=weekly==2928
gen weekafter4202017=weekly==2980
gen blackfriday2015=weekly==2907
gen blackfriday2016=weekly==2959
gen weekafterblackfriday2015=weekly==2908
gen weekafterblackfriday2016=weekly==2960
egen meandiff=mean(pricediff), by(location weekly)

egen meandiffti=mean(pricediffti), by(location weekly)

gen weight6=usableweight+L.usableweight

***STATISTICS FOR MODEL (3 months pre-reform): 
summ tipricepergram_pn pricepergram_pn if firstsaledate<2886-8&lastsaledate>=2886+8&run>=-14&run<0  [aw=usableweight]


***PERMUTATION TEST FIGURE (FIG A.4 - RETAIL PORTION):
*create many placebos:
matrix estimatesfig=[.,.,.,.,.,.] 
set more off
sort groupx weekly
forvalues i=1(1)122{
di `i'
quietly gen TaxChangemany=weekly>=2886-26+`i'
quietly gen taxdiffmany=TaxChangemany-L.TaxChangemany
quietly gen runmany=weekly-(2886-26+`i')
quietly gen sellingweeks=(2886-26+`i')-firstsaledate
quietly gen sellinglastweek=lastsaledate-(2886-26+`i')
quietly reghdfe pricediffti   taxdiffmany i.weeksincefirstsale taxdiff    if abs(runmany)<=6&sellingweeks>=7&sellinglastweek>=7 [aw=weight3], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
*week4202015 week4202016 week4202017 blackfriday2015 blackfriday2016 weekafter* 
*&abs(meandiff)<.1
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=2886-26+`i'
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
quietly drop TaxChangemany taxdiffmany runmany sellingweeks sellinglastweek
}
matrix list estimatesfig
mat2txt, matrix(estimatesfig) saving(retailestimatesbyweek)   replace
import delimited "retailestimatesbyweek.txt", encoding(ISO-8859-1) clear
rename c1 weekly
rename c2 estimate
*drop if weekly==2886|weekly==2875|weekly==2927
rename c3 cilb
rename c4 ciub
rename c5 se
drop if se==0|se==.

*make same drop as for manufacturing data (a lot less retail data during this time too):
drop if weekly<2871

*do a more symmetric drop for holidays:
*2015 4/20: 2875
drop if abs(weekly-2875)<=2
*2016 4/20: 2927
drop if abs(weekly-2927)<=2
*2017 4/20: 2979
drop if abs(weekly-2979)<=2
*tax reform:
drop if abs(weekly-2886)<=2&weekly!=2886 /*keep tax reform period for subsequent stuff*/
*2015 Black Friday: 2907  
drop if abs(weekly-2907)<=2
*2016 Black Friday:  2959
drop if abs(weekly-2959)<=2

gen estimate2=estimate
replace estimate2=estimate+.0693 if weekly==2886
gen tstat=estimate/se
summ estimate2 tstat if weekly==2886

count if estimate<.
di 1/84 /*permutation test*/
twoway (hist estimate if weekly!=2886, start(-0.01)  freq fcolor(gs12) lcolor(white) width(.0025) ), ///
 xline(0.026, lpattern(dash) lcolor(gs8)) graphregion(color(white)) bgcolor(white) xtitle({bf: Tax-Inclusive Estimates}) ytitle({bf: Frequency}) title({bf: Retail Permutation Test})
gr export  hist_retailestpermutation.png, replace as(png)



 cd "${dataclean}"
use incidence_priceanal_small.dta, clear
 set scheme s1manual

 drop if tipricepergram_pn>42

gen weight6=usableweight+L.usableweight
gen Fweight6=usableweight+L2.usableweight

mmerge location weekly using  "shares_comps_10mile_weekly.dta", type(n:1) unmatched(master) ukeep(mkt_sales num_comp share) umatch(source_loc week)


drop F*Placebo F*placebo*  
xtset groupx weekly
gen FPlacebo=weekly>=2887+52
gen Fplacebodiff=0
replace Fplacebodiff=FPlacebo-L.FPlacebo


gen F2Placebo=weekly>=2888+52
gen F2placebodiff=0
replace F2placebodiff=F2Placebo-L.F2Placebo

 gen LPlacebo=weekly>=2885+52
gen Lplacebodiff=0
replace Lplacebodiff=LPlacebo-L.LPlacebo 

 gen L2Placebo=weekly>=2884+52
gen L2placebodiff=0
replace L2placebodiff=L2Placebo-L.L2Placebo 

 gen L3Placebo=weekly>=2883+52
gen L3placebodiff=0
replace L3placebodiff=L3Placebo-L.L3Placebo  

gen F3Placebo=weekly>=2889+52
gen F3placebodiff=0
replace F3placebodiff=F3Placebo-L.F3Placebo 

gen F4Placebo=weekly>=2890+52
gen F4placebodiff=0
replace F4placebodiff=F4Placebo-L.F4Placebo 

gen L4placebogroup=weekly<=2882+52

 cd "${output}"

 ***TABLE 5:
 
set more off
eststo tab2reg1: reghdfe pricediff  taxdiff i.weeksincefirstsale  if abs(run)<=6 [aw=weight3] , noabsorb vce(cluster procloc location)
test taxdiff=-.0693
estadd scalar pval=r(p)
eststo tab2reg2: reghdfe pricediff taxdiff i.weeksincefirstsale  if abs(run)<=6 [aw=weight3] , absorb(group_s##procloc##location) vce(cluster procloc location)
test taxdiff=-.0693
estadd scalar pval=r(p)
eststo tab2reg3: reghdfe pricediff taxdiff i.weeksincefirstsale   if abs(run)<=6 [aw=weight3] , absorb(group_s##procloc##location##weightgroup) vce(cluster procloc location)
test taxdiff=-.0693
estadd scalar pval=r(p)
eststo tab2reg4: reghdfe pricediff  taxdiff   i.weeksincefirstsale  if abs(run)<=6 [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
test taxdiff=-.0693
estadd scalar pval=r(p)
eststo tab2reg5: reghdfe pricediff  taxdiff pricechange taxdiff_pricechange i.weeksincefirstsale  if abs(run)<=6 [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
test taxdiff+taxdiff_pricechange=-.0696
estadd scalar pval=r(p)
eststo tab2reg6: reghdfe pricediff2wk  Ftaxdiff i.weeksincefirstsale   if abs(run)<=6&week1==0 [aw=Fweight3] , absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
test Ftaxdiff=-.0693
estadd scalar pval=r(p)
eststo tab2reg9: reghdfe pricediff2  taxdiff   i.weeksincefirstsale  if abs(run)<=6 [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
test taxdiff=-.64
estadd scalar pval=r(p)

*now repeat for placebo:
   eststo pltab2reg1: reghdfe pricediff  placebodiff i.weeksincefirstsale   if abs(prun)<=6 [aw=weight3] , noabsorb vce(cluster procloc location)
   test placebodiff=-.0693
estadd scalar pval=r(p)
  eststo pltab2reg2: reghdfe pricediff  placebodiff i.weeksincefirstsale   if abs(prun)<=6 [aw=weight3] , absorb(group_s##procloc##location  ) vce(cluster procloc location)
 test placebodiff=-.0693
estadd scalar pval=r(p)
 eststo pltab2reg3: reghdfe pricediff placebodiff i.weeksincefirstsale   if abs(prun)<=6 [aw=weight3] , absorb(group_s##procloc##location##weightgroup ) vce(cluster procloc location)
 test placebodiff=-.0693
estadd scalar pval=r(p)
 eststo pltab2reg4: reghdfe pricediff  placebodiff i.weeksincefirstsale  if abs(prun)<=6  [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i ) vce(cluster procloc location)
 test placebodiff=-.0693
estadd scalar pval=r(p)
 eststo pltab2reg5: reghdfe pricediff placebodiff pricechange2  placebodiff_pricechange i.weeksincefirstsale   if abs(prun)<=6  [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i  ) vce(cluster procloc location)
 test placebodiff+placebodiff_pricechange=-.0696
estadd scalar pval=r(p)
 eststo pltab2reg6: reghdfe pricediff2wk  Fplacebodiff i.weeksincefirstsale  if abs(prun)<=6&week1placebo==0 [aw=Fweight3] , absorb(group_s##procloc##location##weightgroup##group_i  ) vce(cluster procloc location)
test Fplacebodiff=-.0693
estadd scalar pval=r(p)
 eststo pltab2reg9: reghdfe pricediff2  placebodiff i.weeksincefirstsale  if abs(prun)<=6  [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i ) vce(cluster procloc location)
 test placebodiff=-.0693
estadd scalar pval=r(p)

*creates first-diff of first week variables:
 preserve
 keep if firstsaleweek==weekly
 keep if weeklywh==weekly
 count
 egen group_rpsw=group(location procloc location weightgroup)
 collapse (sum) usableweight (mean) logprice_pn logpricewh pricepergram_pn pricepergramwh usableweightwh *TaxChange Placebo L4group run prun firstsaleweek weeklywh location procloc  [aw=usableweight], by(group_rpsw weekly)
 xtset group_rpsw weekly
 gen weight3=L.usableweight
 replace weight3=L2.usableweight if weight3==.
 replace weight3=L3.usableweight if weight3==.
 replace weight3=L4.usableweight if weight3==.
 gen weight6=usableweight+L.usableweight
replace weight6=usableweight+L2.usableweight if weight6==.
replace weight6=usableweight+L3.usableweight if weight6==.
replace weight6=usableweight+L4.usableweight if weight6==.
 gen pricediff_firstweek=logprice_pn - L.logprice_pn
replace pricediff_firstweek=logprice_pn - L2.logprice_pn if pricediff_firstweek==.
replace pricediff_firstweek=logprice_pn - L3.logprice_pn if pricediff_firstweek==.
replace pricediff_firstweek=logprice_pn - L4.logprice_pn if pricediff_firstweek==.
 gen pricewhdiff_firstweek=logpricewh-L.logpricewh
 replace pricewhdiff_firstweek=logpricewh-L2.logpricewh if pricewhdiff_firstweek==.
 replace pricewhdiff_firstweek=logpricewh-L3.logpricewh if pricewhdiff_firstweek==.
 replace pricewhdiff_firstweek=logpricewh-L4.logpricewh if pricewhdiff_firstweek==.
 gen taxdiff_firstweek=TaxChange-L.TaxChange
replace taxdiff_firstweek=TaxChange-L2.TaxChange if taxdiff_firstweek==.
replace taxdiff_firstweek=TaxChange-L3.TaxChange if taxdiff_firstweek==.
replace taxdiff_firstweek=TaxChange-L4.TaxChange if taxdiff_firstweek==.
 gen placebodiff_firstweek=Placebo-L.Placebo
replace placebodiff_firstweek=Placebo-L2.Placebo if placebodiff_firstweek==.
replace placebodiff_firstweek=Placebo-L3.Placebo if placebodiff_firstweek==.
replace placebodiff_firstweek=Placebo-L4.Placebo if placebodiff_firstweek==.

 summ pricediff_* pricewhdiff_*

  
gen difflength=1
replace difflength=2 if logprice_pn -L.logprice_pn ==.
replace difflength=3 if logprice_pn -L2.logprice_pn ==.&logprice_pn -L.logprice_pn ==.
replace difflength=4 if logprice_pn -L3.logprice_pn ==.&logprice_pn -L2.logprice_pn ==.&logprice_pn -L.logprice_pn ==.

eststo tab2reg7: reghdfe pricediff_firstweek taxdiff_firstweek i.difflength    if abs(run)<=6&firstsaleweek==weekly&abs(weeklywh-weekly)<=0 [aw=weight3] , absorb(group_rpsw) vce(cluster location procloc)
test taxdiff_firstweek=-.0693
estadd scalar pval=r(p)
 eststo tab2reg8: reghdfe pricediff_firstweek taxdiff_firstweek pricewhdiff_firstweek i.difflength     if abs(run)<=6&firstsaleweek==weekly&abs(weeklywh-weekly)<=0 [aw=weight3], absorb(group_rpsw) vce(cluster location procloc)
 test taxdiff_firstweek=-.0693
estadd scalar pval=r(p)

eststo pltab2reg7: reghdfe pricediff_firstweek placebodiff_firstweek i.difflength    if abs(prun)<=6&firstsaleweek==weekly&abs(weeklywh-weekly)<=0 [aw=weight3] , absorb(group_rpsw) vce(cluster location procloc)
test placebodiff_firstweek=-.0693
estadd scalar pval=r(p)
 eststo pltab2reg8: reghdfe pricediff_firstweek placebodiff_firstweek pricewhdiff_firstweek i.difflength     if abs(prun)<=6&firstsaleweek==weekly&abs(weeklywh-weekly)<=0 [aw=weight3], absorb(group_rpsw) vce(cluster location procloc)
 test placebodiff_firstweek=-.0693
estadd scalar pval=r(p)

 
 restore

 
esttab tab2reg1 tab2reg4 tab2reg6  tab2reg9 tab2reg7 tab2reg8   , keep(*taxdiff*   pricewhdiff_firstweek ) star(* 0.05 ** 0.01 *** 0.001) se(%9.5f) stats(N N_clust1 N_clust2 r2_a r2_a_within meanpre pctchange pval, fmt(0 0 0 3 3 3 3))
esttab pltab2reg1 pltab2reg4 pltab2reg6 pltab2reg9 pltab2reg7 pltab2reg8 , keep(*placebodiff*   pricewhdiff_firstweek ) star(* 0.05 ** 0.01 *** 0.001) se(%9.5f) stats(N N_clust1 N_clust2 r2_a r2_a_within meanpre pctchange pval, fmt(0 0 0 3 3 3 3))
 
estout  tab2reg1 tab2reg4 tab2reg6 tab2reg9 tab2reg7 tab2reg8   using retail_tab2.tex, cells(b(star fmt(3)) se(par)) replace keep(*taxdiff*  pricewhdiff_firstweek) style(tex)  starlevels(* 0.05 ** 0.01 *** 0.001) stats(N N_clust1 N_clust2 r2_a meanpre pctchange pval, fmt(0 0 0 3 3 3))
estout pltab2reg1 pltab2reg4 pltab2reg6 pltab2reg9 pltab2reg7 pltab2reg8 using retail_placebotab2.tex, cells(b(star fmt(3)) se(par)) replace keep(*placebodiff*   pricewhdiff_firstweek) style(tex)  starlevels(* 0.05 ** 0.01 *** 0.001) stats(N N_clust1 N_clust2 r2_a meanpre pctchange pval, fmt(0 0 0 3 3 3 3))

    
*FIGURE WITH LEADS AND LAGS (FIGURE A.2 - RETAIL PORTION): 
*main event study:
set more off
eststo tab3reg1: reghdfe pricediff  L3taxdiff L2taxdiff taxdiff Ftaxdiff F2taxdiff F3taxdiff L4group F4TaxChange Ltaxdiff i.weeksince  if abs(run)<=6 [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
coefplot ,  graphregion(color(white)) bgcolor(white) xtitle({bf: Tax Reform Coefficients}) ytitle({bf: Tax-Exclusive First-Differenced Price Estimates}) title({bf: Retail Event Study}) vertical yline(0, lpattern(dash) lcolor(black))  keep(L3taxdiff L2taxdiff Ltaxdiff taxdiff Ftaxdiff F2taxdiff F3taxdiff) order(L3taxdiff L2taxdiff Ltaxdiff taxdiff Ftaxdiff F2taxdiff F3taxdiff) coeflabels(L3taxdiff="t - 3" L2taxdiff  ="t - 2" Ltaxdiff= "t - 1" taxdiff = "t" Ftaxdiff= "t + 1" F2taxdiff="t + 2" F3taxdiff="t+3")
gr export  retail_pricediff.png, replace as(png)

*placebo event study:
set more off
eststo tab3reg1: reghdfe  pricediff  L3placebodiff L2placebodiff placebodiff Fplacebodiff F2placebodiff F3placebodiff L4placebogroup F4Placebo Lplacebodiff i.weeksince  if abs(prun)<=6 [aw=weight3] , absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
coefplot ,  graphregion(color(white)) bgcolor(white) xtitle({bf: Placebo Coefficients by Time}) ytitle({bf: Tax-Exclusive First-Differenced Price Estimates}) title({bf: Retail Placebo  Event Study}) vertical yline(0, lpattern(dash) lcolor(black))  keep(L3placebodiff L2placebodiff Lplacebodiff placebodiff Fplacebodiff F2placebodiff F3placebodiff) order(L3placebodiff L2placebodiff Lplacebodiff placebodiff Fplacebodiff F2placebodiff F3placebodiff) coeflabels(L3placebodiff="t - 3" L2placebodiff  ="t - 2" Lplacebodiff= "t - 1" placebodiff = "t" Fplacebodiff= "t + 1" F2placebodiff="t + 2" F3placebodiff="t+3")
gr export  retail_pricediffplacebo.png, replace as(png)


***BANDWIDTH CHECK FIGURE FOR FIRST DIFF (FIGURE A.5 - RETAIL PORTION): 
set more off
gr drop _all
matrix estimatesfig=[.,.,.,.,.,.] 
eststo fig1reg2: reghdfe pricediff taxdiff i.weeksincefirstsale     if abs(run)<=2 [aw=weight3], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
eststo fig1reg3: reghdfe pricediff taxdiff  i.weeksincefirstsale    if abs(run)<=3 [aw=weight3], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ] 
eststo fig1reg4: reghdfe pricediff taxdiff i.weeksincefirstsale     if abs(run)<=4  [aw=weight3], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
eststo fig1reg5: reghdfe pricediff taxdiff  i.weeksincefirstsale    if abs(run)<=5  [aw=usableweight], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
eststo fig1reg6: reghdfe pricediff taxdiff  i.weeksincefirstsale   if abs(run)<=6  [aw=weight3], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
eststo fig1reg7: reghdfe pricediff taxdiff  i.weeksincefirstsale   if abs(run)<=7  [aw=weight3], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
eststo fig1reg8: reghdfe pricediff taxdiff i.weeksincefirstsale   if abs(run)<=8 [aw=weight3], absorb(group_s##procloc##location##weightgroup##group_i) vce(cluster procloc location)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]

matrix estimatesfig=estimatesfig[2...,1...]
*matrix rownames estimatesfig=    2015w27   2016w1 2016w14 2016w27 
matrix rownames estimatesfig= "2 wks" "3 wks"  "4 wks" "5 wks" "6 wks" "7 wks" "8 wks" 
*can add 4,3,2 if drop day of month dummies...but dom dummies matter for weight estimates, particularly when cut in weeks, not months (so non-symmetric days on each side of cutoff)
matrix list estimatesfig
graph drop _all
coefplot (matrix(estimatesfig[,2]), ci((estimatesfig[,3] estimatesfig[,4]))),  graphregion(color(white)) bgcolor(white) xtitle({bf: Bandwidth}) ytitle({bf: First-Differenced Tax-Exclusive Price Estimate}) title({bf: Retail Bandwidth Sensitivity } , color(black))   vertical yline(0, lpattern(dash) lcolor(black))   ysc(r(-.06 0)) ylabel(-.06(.02)0, angle(0) nogrid) 
*gr combine `2'_bwchoicefig3 `2'_bwchoicefig5 `2'_bwchoicefig7, title({bf:`3'}) rows(1)  xsize(13) ysize(4.5) ycommon  graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
gr export  retail_firstdiffbandwidthfig.png, replace as(png)

 
***SUMMARY STATISTICS TABLE (TABLE 2): 
 reg pricediff taxdiff   if abs(run)<=6  [aw=weight3] , cluster(location)
 gen sample=e(sample)
 gen mkup=pricepergram_pn/pricepergramwh
 replace mkup=mkup-1
 
  gen centsfortaxinvar=abs(tipricepergram_pn /(.37+ taxmultsales)-tipricepergram_pn/(1.25*taxmultsales))
     gen centspct=log((pricepergramwh-centsfortaxinvar)/pricepergramwh)
   gen taxrevpre=(tipricepergram_pn/(1.25*taxmultsales))*.25+(tipricepergram_pn/(taxmultsales))*(taxmultsales-1)+.25*pricepergramwh
 gen taxrevpost=(tipricepergram_pn/(.37+ taxmultsales))*(.37+taxmultsales-1) 
 
 *create vars to make centsfortaxinvar in proc anal:
 egen group_rps=group(location procloc strain_f)
   egen tmpx=total(tipricepergram_pn*usableweight) if TaxChange==0&abs(run)<=8&run<0, by(group_rps)
   egen totweightprerps=total(usableweight) if TaxChange==0&abs(run)<=8&run<0, by(group_rps)
   egen meanretailpre=max(tmpx/totweightprerps), by(group_rps)
 egen meanwholesalepre=max(tmpx/totweightprerps), by(group_rps)
   drop tmpx
   egen tmpx=total(tipricepergram_pn*usableweight) if TaxChange==0&abs(run)<=8&run<0&firstsaleweek==weekly, by(group_rps)
   egen totweightprefirstweek=total(usableweight) if TaxChange==0&abs(run)<=8&run<0&firstsaleweek==weekly, by(group_rps)
   egen meanretailfirstweekpre=max(tmpx/totweightprefirstweek), by(group_rps)
   drop tmpx 
   
	  *placebo:
   egen tmpx=total(tipricepergram_pn*usableweight) if Placebo==0&abs(prun)<=8&prun<0, by(group_rps)
   egen totweightplaceborps=total(usableweight) if Placebo==0&abs(prun)<=8&prun<0, by(group_rps)
   egen meanretailplacebo=max(tmpx/totweightplaceborps), by(group_rps)
   drop tmpx
   
    cd "${dataclean}"
 preserve
    gen tmpx=taxmultsales if weekly==2885
 egen retailsaletaxmultpre=max(tmpx), by(location)
egen tagrps=tag(group_rps)
keep if tagrps==1
keep location procloc strain_final   meanretailpre retailsaletaxmultpre meanretailplacebo meanwholesalepre meanretailfirstweekpre
save meanretailpre_forfirstdiff.dta, replace
restore
 cd "${output}"
 
 
 egen tmpx=total(usableweight), by(procloc weekly)
egen procsize=max(tmpx), by(procloc weekly)
drop tmpx
    egen tagprocx=tag(procloc weekly) if procsize<.
  egen tmpx2=total(procsize) if tagprocx==1, by(weekly)
  egen totmkt=max(tmpx2), by(weekly)
  gen procmktshare=procsize/totmkt
 
/*egen tag_pr=tag(location procloc) if TaxChange==0&abs(run)<=8
egen firmssellproc=total(tag_pr), by(procloc)
egen firmsbuyretail=total(tag_pr), by(location)*/

gen priceincrease=pricediff>.01&pricediff<.&abs(run)<=6&TaxChange==0
gen pricedecrease=pricediff<-.01&pricediff<.&abs(run)<=6&TaxChange==0

 
sutex tipricepergram_pn  pricepergram_pn priceincrease pricedecrease pricepergramwh taxmultsales taxrevpre share procmktshare numproc4 taxrevpost centsfortaxinvar centspct   [aw=usableweight]    if TaxChange==0 &abs(run)<=6&sample==1 , minmax
di (.25*9.304 )+.089*(9.304 *1.25)+.25*4.048 
di (.37*9.304 )+.089*(9.304  ) 


